package cn.itcast.controller;

import cn.itcast.domain.Accounts;
import cn.itcast.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {

        @Autowired
        private AccountService accountService;

        @GetMapping
        public Object findAll()throws Exception{
            List<Accounts> all = accountService.findAll();
            return all;
        }
        @GetMapping("/{pageNum}/{pageSize}")
        public Object findPageAll( @PathVariable Integer pageNum, @PathVariable Integer pageSize)throws Exception{

            return   accountService.pageQure(pageNum,pageSize);
        }
        @GetMapping("/{id}")
        public Object findById(@PathVariable Integer id)throws Exception{
            return accountService.findById(id);
        }

        @PostMapping
        public Object add(@Validated  @RequestBody Accounts accounts, Errors errors)throws Exception{
            HashMap list = new HashMap<>();
            HashMap list2 = new HashMap<>();

            if (errors.hasErrors()){
                for (FieldError fieldError : errors.getFieldErrors()) {
                    list.put(fieldError.getField(),fieldError.getDefaultMessage());
                }

            }
            if (list.size()==0){
                accountService.add(accounts);
                list2.put("account",true);
            }else {
                list2.put("account",false);
                list2.put("errors",list);
            }

            return list2;
        }

        @DeleteMapping("/{id}")
        public Object delete( @PathVariable Integer id,Errors errors){
            HashMap list = new HashMap<>();
            HashMap list2 = new HashMap<>();

            if (errors.hasErrors()){
                for (FieldError fieldError : errors.getFieldErrors()) {
                    list.put(fieldError.getField(),fieldError.getDefaultMessage());
                }

            }
            if (list.size()==0){
                try {
                    accountService.delete(id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                list2.put("account",true);
            }else {
                list2.put("account",false);
                list2.put("errors",list);
            }

            return list2;
        }
    @PutMapping
    public Object update(@Validated  @RequestBody Accounts accounts, Errors errors)throws Exception{
        HashMap list = new HashMap<>();
        HashMap list2 = new HashMap<>();

        if (errors.hasErrors()){
            for (FieldError fieldError : errors.getFieldErrors()) {
                list.put(fieldError.getField(),fieldError.getDefaultMessage());
            }

        }
        if (list.size()==0){
            accountService.update(accounts);
            list2.put("account",true);
        }else {
            list2.put("account",false);
            list2.put("errors",list);
        }

        return list2;
    }
}
